package com.caac.core.config;

import java.util.Map;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Expression;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

public class NotEqualOperator extends Operator {
    public void init(Object bean, Map aliasMap, Map propMap, DmProperty dp) {
        BeanWrapper bw = new BeanWrapperImpl(bean);
        Object value = bw.getPropertyValue(dp.getChild(0).getName());
        if (value == null || "".equals(value)) {
            return;
        }
        if (dp.getAlias().isHasAlias()) {
            aliasMap.put(dp.getAlias().getParentName(), dp.getAlias().getPrefix());
        }
        propMap.put(dp.getName(), new Object[] {value});
    }

    public Criterion getCriterion(Map propMap, DmProperty dp) {
        Object[] objects = (Object[]) propMap.get(dp.getName());
        if (objects == null) {
            return null;
        }
        return Expression.ne(dp.getAlias().getName(), objects[0]);
    }
}
